﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoCache.Aggregation.Interfaces;
using Fsb.Utility;

namespace VideoCache.Aggregation.Implements
{
    [Site("pps.tv")]
    class PPSFetcher
    {
        public string GetToken(string url)
        {
            string token = this.GetTokenByUrl(url);

            if (string.IsNullOrEmpty(token))
            {
                return this.GetTokenByPlayer(url);
            }

            return token;
        }

        string GetTokenByUrl(string url)
        {
            string html = HttpUtility.Get(url, Encoding.GetEncoding("gb2312"));

            string json = StringUtility.SubString(html, "var video = ", ";");

            Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(json);

            if (jObject != null)
            {
                return jObject["video_id"].ToString().Trim('\"');
            }

            return null;
        }

        string GetTokenByPlayer(string url)
        {
            return UrlHelper.GetParameter(url, "video_id");
        }
    }
}
